package eerbaar.v2.effects.internal;

import java.awt.Color;
import java.awt.Graphics2D;

import eerbaar.v2.effects.Effect;

public class RisingText implements Effect {

    private final String text;
    private final TimeLine timeLine;
    private final Color color;

    public RisingText(String text, CanvasPosition start, Color color,
            int timeOut) {
        this.text = text;
        this.color = color;
        CanvasPosition pos = calcCenter(start);
        this.timeLine = EffectsFactory.createRisingTimeLine(pos, timeOut);
    }

    private CanvasPosition calcCenter(CanvasPosition start) {
        double left = start.getX();
        left -= (calcTextWidgth() / 2);
        return new CanvasPosition(left, start.getY());
    }

    private int calcTextWidgth() {
        int length = this.text.length();
        return (length * 8);
    }

    @Override
    public void display(int time, Graphics2D canvas) {

        CanvasPosition p = this.timeLine.getCanvasPosition();
        if (p == null) {
            return;
        }

        canvas.setColor(this.color);
        int x = (int) p.getX();
        int y = (int) p.getY();
        canvas.drawString(this.text, x, y);
    }

    @Override
    public boolean isExpired(int time) {

        CanvasPosition p = this.timeLine.getCanvasPosition();
        if (p == null) {
            return true;
        }

        return false;
    }

    @Override
    public void setExpired(boolean expired) {
        //
    }

}
